/*********************************************************************
Replication code for Systemic Discrimination Among Large U.S. Employers
Patrick M. Kline, Evan K. Rose, Christopher R. Walters
April, 2022

This code produces the estimates of correlations of job- and firm-level
discrimination estimates with observable characteristics of jobs, firms,
and industries.

It requires top-level directory be set to the replication folder using
the global below.
*********************************************************************/

global dir "/accounts/projects/pkline/randres/randres/replication"
capture restore
clear all
set seed 126312

* Load the data
use ${dir}/data/data.dta, clear

* Indicators for regions
foreach k of numlist 1/4 {
    gen region4_`k' = region4 == `k'
}

*** Covariates
* Add black group share black from wac
preserve 
insheet using ${dir}/covariates/wac_block_2015-2017.csv, comma clear
drop firm_id
xtile blackq=frac_black, nquantiles(5)
tempfile wac_block
save `wac_block', replace
restore

* Add black share in from county
preserve 
insheet using ${dir}/covariates/wac_county_2015-2017.csv, comma clear
drop state_fips county_fips
foreach var of varlist frac_* {
    rename `var' `var'_county
}
tempfile wac_county
save `wac_county', replace
restore

* Reference USA data
preserve
insheet using ${dir}/covariates/refusa.csv, comma clear
drop firm_id state
gen nonwhite_manager_share = 1 - white_manager_share
tempfile refusa
save `refusa', replace
restore

* Firm-level reference USA data
preserve 
insheet using ${dir}/covariates/refusa_managers.csv, comma clear
replace executiveethnicity = "" if ~inlist(executivetitle,"Manager","Site Manager","Office Manager","General Mgr")
foreach k of numlist 1/100 {
    capture replace executiveethnicity`k' = "" if !inlist(executivetitle`k',"Manager","Site Manager","Office Manager","General Mgr")
    capture replace executivegender`k' = "" if !inlist(executivetitle`k',"Manager","Site Manager","Office Manager","General Mgr")
}
gen nmanager = 0
gen nwhite = 0
gen nblack = 0
foreach var of varlist executiveethnicity* {
    replace nmanager = nmanager + 1 if `var' != ""
    replace nwhite = nwhite + 1 if inlist(`var', "Western Europe","Eastern European","Scandinavian","Mediterranean")
    replace nblack = nblack + 1 if inlist(`var', "African American", "Hispanic")
}

gen nmale = 0
gen nfemale = 0
foreach var of varlist executivegender* {
    replace nmale = nmale + 1 if inlist(`var', "Male")
    replace nfemale = nfemale + 1 if inlist(`var', "Female")
}

collapse (sum) nmanager nwhite nblack nmale nfemale, by(firm_id)
gen firm_share_white_mgmt = nwhite / nmanager
gen firm_share_nonwhite_mgmt = 1 - firm_share_white_mgmt
gen firm_share_black_mgmt = nblack / nmanager
gen firm_share_male_mgmt = nmale / nmanager
gen firm_share_female_mgmt = nfemale / nmanager
drop nmanager nwhite nblack nmale nfemale
tempfile refusafirm
save `refusafirm', replace
restore


* IAT data
preserve 
insheet using ${dir}/covariates/jobs_iat_animus.csv, comma clear
keep job_id iat_all animus_dma_std_all animus_state_std_all
foreach var of varlist iat_all animus_dma_std_all animus_state_std_all {
    destring `var', force replace
}
tempfile iat
save `iat', replace
restore

preserve 
insheet using ${dir}/covariates/iat_gender_stats.csv, comma clear
collapse (mean) iat_gender_st, by(job_id)
tempfile iat_gend
save `iat_gend', replace
restore

* Glass door covariates
preserve
insheet using ${dir}/covariates/glassdoor.csv, comma clear
drop randres_index scrape_date company randres_name
tempfile gd
save `gd', replace
restore

* DOL covariates
preserve
insheet using ${dir}/covariates/dol_whd.csv, comma clear
tempfile dol
save `dol', replace
restore

* Other violations
preserve
insheet using ${dir}/covariates/violations.csv, comma clear
keep if primaryoffense == "employment discrimination"
collapse (sum) penaltyamount (count) npenalties = penaltyamount, by(firm_id)
tempfile viols
save `viols', replace
restore

* Federal contractors
preserve
insheet using ${dir}/covariates/federal_contractors.csv, comma clear names
destring firm_id, force replace
drop if firm_id == .
tempfile ofccp
save `ofccp', replace
restore

* DnB covariates
preserve
insheet using ${dir}/covariates/dnb_2010-2018.csv, comma clear
drop if sls == . | employeesallsites == .
* bys firm_id: egen last_year = max(year)
* keep if year == last_year
* drop year
collapse (sum) sls employeesallsites, by(firm_id)
tempfile dnb
save `dnb', replace
restore

* Compustat
preserve
insheet using ${dir}/covariates/compustat_2010-2018.csv, comma clear
drop if emp == . | gp == .
* bys firm_id: egen last_year = max(year)
* keep if year == last_year
* drop year gvkey coname
collapse (sum) emp gp, by(firm_id)
tempfile compu
save `compu', replace
restore

* Manual firm size
preserve
insheet using ${dir}/covariates/firm_size.csv, comma clear
rename id firm_id
rename ofemployees firm_size
keep firm_id firm_size
tempfile firm_size
save `firm_size', replace
restore

* Boards
preserve
insheet using ${dir}/covariates/board_diversity_2014-2019.csv, comma clear
drop firm gvkey dunsno boardexno 
collapse (sum) *_members, by(firm_id)
tempfile board
save `board', replace
restore

* C-suit
preserve
insheet using ${dir}/covariates/csuite_diversity_2021.csv, comma clear
gen csuite_black_share = blk_csuite_execs / csuite_execs
gen csuite_female_share = fem_csuite_execs / csuite_execs
keep firm_id csuite_black_share csuite_female_share
tempfile csuite
save `csuite', replace
restore

* Chief diveristy offcers
preserve
insheet using ${dir}/covariates/csuite_cdo.csv, comma clear
collapse (max) diversity_officer, by(firm_id)
tempfile cdo
save `cdo', replace
restore

* Onet data
preserve
insheet using "${dir}/covariates/onet_data.tsv", tab clear
foreach var of varlist analytical routine social soc_interaction manual cust_interaction {
    rename `var' score_`var'
}

keep job_id randres_job_title onet_job_title score_*
drop if score_analytical == .
duplicates drop job_id onet_job_title, force
tempfile onet
save `onet', replace
restore

* EEOC industy data
preserve
insheet using ${dir}/covariates/black_employment_shares_3dig.csv, comma clear
rename share_black share_black_ind
rename share_blackmidoffandmanagers share_black_mgmt_ind
rename share_blacksenioroffandmanagers share_black_senior_mgmt_ind
gen mgmt_dif_black_ind = share_black_mgmt_ind - share_black_ind

rename share_female share_female_ind
rename share_femalemidoffandmanagers share_female_mgmt_ind
rename share_femalesenioroffandmanagers share_female_sr_mgmt_ind
gen mgmt_dif_female_ind = share_female_mgmt_ind - share_female_ind
keep naics* *_ind
tempfile eeoc
save `eeoc', replace
restore

* Concentration
preserve
insheet using ${dir}/covariates/concentration_top4.csv, comma clear
rename val_pct top4share
rename naics2017 naics3
keep naics3* top4share
destring naics3, force replace
drop if naics3 == .
tempfile concent
save `concent', replace
restore

preserve
insheet using ${dir}/covariates/concentration.csv, comma clear
keep if concenfi == 604
rename val_pct top4share
rename naics2017 naics
keep naics top4share
destring naics, force replace
destring top4share, force replace
drop if naics == . | top4share == .
tempfile concent_naics
save `concent_naics', replace
restore

* Wage / college gaps
preserve
use ${dir}/covariates/naics3_race_wage_gap.dta, clear
rename gap_coef wage_gap_race
rename gap_se wage_gap_race_se
rename ind_coef wage_level_race
rename ind_se wage_level_race_se
replace wage_gap_race = -wage_gap_race
merge 1:1 naics using ${dir}/covariates/naics3_gender_wage_gap.dta, nogen
rename gap_coef wage_gap_gender
rename gap_se wage_gap_gender_se
rename ind_coef wage_level_gender
rename ind_se wage_level_gender_se
replace wage_gap_gender = -wage_gap_gender

tempfile wagegap_naics
save `wagegap_naics', replace
restore

preserve
use ${dir}/covariates/sic_race_wage_gap.dta, clear
rename coef wage_gap_race
rename se wage_gap_race_se
replace wage_gap_race = -wage_gap_race
merge 1:1 sic using  ${dir}/covariates/sic_gender_wage_gap.dta, nogen
rename coef wage_gap_gender
rename se wage_gap_gender_se
replace wage_gap_gender = -wage_gap_gender

rename sic sic_code
expand 2 if sic_code == 63
replace sic_code = 64 if _n == _N
tempfile wagegap
save `wagegap', replace
restore

preserve
use ${dir}/covariates/sic_race_wage_gap_basic.dta, clear
rename coef wage_gap_race_basic
rename se wage_gap_race_basic_se
replace wage_gap_race_basic = -wage_gap_race_basic
merge 1:1 sic using  ${dir}/covariates/sic_gender_wage_gap_basic.dta, nogen
rename coef wage_gap_gender_basic
rename se wage_gap_gender_basic_se
replace wage_gap_gender_basic = -wage_gap_gender_basic

rename sic sic_code
expand 2 if sic_code == 63
replace sic_code = 64 if _n == _N
tempfile rawgap
save `rawgap', replace
restore

preserve
use ${dir}/covariates/sic_shares.dta, clear
rename sic sic_code
expand 2 if sic_code == 63
replace sic_code = 64 if _n == _N
rename college_share_gap_race col_share_gap_race
rename college_share_gap_gender col_share_gap_gender
keep sic_code emp_gap_race emp_gap_gender col_share_gap_race col_share_gap_gender
foreach var of varlist emp_gap_race emp_gap_gender col_share_gap_race col_share_gap_gender {
    replace `var' = -`var'
}
tempfile sicshares
save `sicshares', replace
restore

preserve
use ${dir}/covariates/naics3_shares.dta, clear
rename college_share_gap_race col_share_gap_race
rename college_share_gap_gender col_share_gap_gender
keep naics3 emp_gap_race emp_gap_gender col_share_gap_race col_share_gap_gender
foreach var of varlist emp_gap_race emp_gap_gender col_share_gap_race col_share_gap_gender {
    replace `var' = -`var'
}
tempfile shares_naics
save `shares_naics', replace
restore

* Phones per job
preserve
collapse (mean) phones_per_job phones_per_job_other, by(job_id)
tempfile decentral
save `decentral', replace
restore


** Sum stats for firm-level covariates
preserve
collapse (mean) phones_per_job, by(firm_id naics3 naics sic_code)
duplicates drop

* Add covars
merge m:1 firm_id using `dol', keep(3 1) nogen
merge m:1 firm_id using `viols', keep(3 1) nogen
merge m:1 firm_id using `dnb', keep(3 1) nogen
merge m:1 firm_id using `compu', keep(3 1) nogen
merge m:1 firm_id using `board', keep(3 1) nogen
merge m:1 firm_id using `gd', keep(3 1) nogen
merge m:1 firm_id using `firm_size', keep(3 1) nogen
merge m:1 firm_id using `refusafirm', keep(3 1) nogen
merge m:1 naics3 using `eeoc', keep(3 1) nogen
merge m:1 naics using `concent_naics', keep(3 1) nogen
merge m:1 sic_code using `wagegap', keep(3 1) nogen
merge m:1 sic_code using `rawgap', keep(3 1) nogen
merge m:1 sic_code using `sicshares', keep(3 1) nogen
merge m:1 firm_id using `csuite', keep(3 1) nogen
merge m:1 firm_id using `cdo', keep(3 1) nogen
merge m:1 firm_id using `ofccp', keep(3 1) nogen
gen log_firm_emp = log(firm_size)
gen viols_per_emp = case_violtn_cnt / (firm_size)
gen penalties_per_emp = npenalties / (firm_size/1000)
replace penalties_per_emp = 0 if penalties_per_emp == .
gen sls_per_emp = sls / (employeesallsites*1000)
gen profit_per_emp = gp / (emp*1000)
gen sales_margin = gp / sls
gen black_board_share = blk_brd_members / brd_members
gen female_board_share = fem_brd_members / brd_members
gen any_black = blk_brd_members > 0
gen two_black = blk_brd_members > 1
gen score_diff = diversity_score - overall_score
gen fed_contract = contractor != "No"

label var log_firm_emp "\quad Log employment"
label var viols_per_emp "\quad DOL viols / emp" 
label var penalties_per_emp "\quad Empl-discr cases / thousand emp"
label var sls_per_emp "\quad Sales / emp (\$M)" 
label var profit_per_emp "\quad Profit / emp (\$M)" 
label var sales_margin "\quad Profit / sales" 
label var black_board_share "\quad \% board Black"
label var female_board_share "\quad \% board female"
label var csuite_black_share "\quad \% c-suite Black"
label var csuite_female_share "\quad \% c-suite female"
label var diversity_officer "\quad Has chief diversity officer"
label var share_black_ind "\quad \% ind Black"
label var share_black_mgmt_ind "\quad \% ind mgmt Black"
label var overall_score "\quad GD score"
label var diversity_score "\quad GD diversity score"
label var phones_per_job "\quad Callback centralization"
label var firm_share_white_mgmt "\quad \% managers white"
label var firm_share_nonwhite_mgmt "\quad \% managers non-white"
label var firm_share_male_mgmt "\quad \% managers male"
label var firm_share_female_mgmt "\quad \% managers female"
label var fed_contract "\quad Federal contractor"

* Run sumstats
global covars log_firm_emp sls_per_emp profit_per_emp overall_score viols_per_emp penalties_per_emp fed_contract black_board_share female_board_share firm_share_nonwhite_mgmt firm_share_female_mgmt diversity_officer diversity_score phones_per_job
replace phones_per_job = -phones_per_job

eststo clear
eststo: quietly estpost summarize ${covars}, detail
esttab using ${dir}/tables/tableC3.tex, replace cells("mean(fmt(3)) sd(fmt(2)) p50(fmt(3))") collabels("Mean" "SD" "Median") label nomtitles refcat(log_firm_emp "\textbf{Firm performance}" viols_per_emp "\textbf{Legal compliance}" black_board_share "\textbf{Firm diversity}" phones_per_job "\textbf{Callback patterns}", nolabel)
restore

* Collapse to job level
foreach touse of varlist black female over40 {
    if "`touse'" == "black" {
        local gap = "white-Black"
    }
    else if "`touse'" == "female" {
        local gap = "male-female"
    }
    else {
        local gap = "under-over 40"        
    }
    preserve
    collapse (mean) cb region4_2 region4_3 region4_4 (count) napps=cb, by(wave firm_id firm job_id sic_code naics naics3 region4 `touse')
    reshape wide cb napps, i(firm_id wave job_id) j(`touse')
    drop if napps0 == 0 | napps0 == .
    drop if napps1 == 0 | napps1 == .
    gen race_dif = cb0 - cb1
    drop napps*

    * Other constructed covariates
    merge 1:1 job_id using `wac_block', nogen keep(1 3)
    merge 1:1 job_id using `wac_county', nogen keep(1 3)
    merge 1:1 job_id using `refusa', nogen keep(1 3)
    merge 1:1 job_id using `iat', nogen keep(1 3)
    merge 1:1 job_id using `iat_gend', nogen keep(1 3)
    merge 1:1 job_id using `onet', nogen keep(1 3)
    merge 1:1 job_id using `decentral', nogen keep(1 3)
    gen logemp = log(locationemployeesizeactual)
    gen race_relative = frac_black - frac_black_county
    bys firm_id: egen mean_logemp = mean(logemp)    
    gen mgmt_relative = nonwhite_manager_share - frac_black
    gen frac_black_county2 = frac_black_county^2

    * firm-level covariates
    merge m:1 firm_id using `dol', keep(3 1) nogen
    merge m:1 firm_id using `viols', keep(3 1) nogen
    merge m:1 firm_id using `dnb', keep(3 1) nogen
    merge m:1 firm_id using `compu', keep(3 1) nogen
    merge m:1 firm_id using `board', keep(3 1) nogen
    merge m:1 firm_id using `gd', keep(3 1) nogen
    merge m:1 firm_id using `firm_size', keep(3 1) nogen
    merge m:1 firm_id using `refusafirm', keep(3 1) nogen
    merge m:1 naics3 using `eeoc', keep(3 1) nogen
    merge m:1 naics using `concent_naics', keep(3 1) nogen
    merge m:1 sic_code using `wagegap', keep(3 1) nogen
    merge m:1 sic_code using `rawgap', keep(3 1) nogen
    merge m:1 sic_code using `sicshares', keep(3 1) nogen
    merge m:1 firm_id using `csuite', keep(3 1) nogen
    merge m:1 firm_id using `cdo', keep(3 1) nogen
    merge m:1 firm_id using `ofccp', keep(3 1) nogen
    gen log_firm_emp = log(firm_size*1000)
    gen viols_per_emp = case_violtn_cnt / (firm_size*1000)
    gen penalties_per_emp = npenalties / (firm_size*1000)
    replace penalties_per_emp = 0 if penalties_per_emp == .
    gen sls_per_emp = sls / (employeesallsites*1000)
    gen profit_per_emp = gp / (emp*1000)
    gen sales_margin = gp / sls
    gen black_board_share = blk_brd_members / brd_members
    gen female_board_share = fem_brd_members / brd_members
    gen any_black = blk_brd_members > 0
    gen two_black = blk_brd_members > 1
    gen score_diff = diversity_score - overall_score
    gen fed_contract = contractor != "No"

    * Establishment level coefficients
    label var frac_black_county "% county Black" 
    label var frac_black_county2 "% county Black{sup:2}" 
    label var frac_female_county "% county female" 
    label var frac_black "% block Black"
    label var frac_female "% block female"
    label var iat_all "County race IAT"
    label var iat_gender_st "County gender IAT"
    label var animus_dma_std_all "DMA animus"
    label var animus_state_std_all "State animus"
    label var white_manager "White manager"
    label var white_manager_share "% managers white"
    label var nonwhite_manager_share "% managers non-white"
    label var male_manager "Male manager"
    label var male_manager_share "% managers male"
    label var logemp "Log emp"
    label var female_manager_share "% managers female"
    label var mean_logemp "Mean log estab size"
    label var region4_2 "Midwest"
    label var region4_3 "South"
    label var region4_4 "West"

    foreach var of varlist logemp mean_logemp {
        qui su `var', d
        replace `var' = (`var' - r(mean))/r(sd)
    }

    eststo clear
    global covars frac_black_county frac_black frac_female iat_all iat_gender_st animus_dma_std_all nonwhite_manager_share female_manager_share logemp region4_2 region4_3 region4_4
    local fes = ""
    foreach var of global covars {
        qui eststo: reg race_dif i.wave `var', robust
        estimates store `var'
        local lab: variable label `var'
        local nobs = e(N)
        label var `var' "`lab' (`nobs')"

        qui eststo: areg race_dif i.wave `var', absorb(firm_id) robust
        estadd local firm_fe "Yes"
        estimates store `var'_fid
        local fes = "`fes' `var'_fid"
    }

    eststo: qui reg race_dif ${covars} i.wave, robust
    testparm ${covars}
    local nfp = r(p)

    areg race_dif ${covars} i.wave, absorb(firm_id) robust
    testparm ${covars}

    coefplot (${covars}, label("Bivariate")) (`fes', label("Firm FE")), drop(_cons *.wave) xline(0) graphregion(color(white)) legend(region(color(white))) xtitle("Effect on job `gap' contact gap") note("P-value for joint sig w/o firm FE: `: di %3.2f `=`nfp''', w/ firm fe: `: di %3.2f `=r(p)''") headings(frac_black_county = "{bf:Local demographics}" iat_all = "{bf:Local sentiment}" nonwhite_manager_share = "{bf:Establishment characteristics}" region4_2 = "{bf:Region}")
    if "`touse'" != "over40" {
        graph export ${dir}/figures/figure4_`touse'.pdf, replace
    }
    else {
        graph export ${dir}/figures/figureA8.pdf, replace
    }

    * Onet covariates
    label var score_analytical "Analytical skills"
    label var score_routine "Routine skills"
    label var score_soc_interaction "Social interaction skills"
    label var score_cust_interaction "Customer interaction skills"
    label var score_manual "Manual skills"

    global covars score_analytical score_routine score_soc_interaction score_cust_interaction score_manual

    * Industry task regs
    ivreg2 race_dif (score_cust_interaction = i.sic_code), cluster(job_id)

    eststo clear
    local fes = ""
    foreach var of global covars {
        qui eststo: areg race_dif i.wave `var', absorb(sic_code) robust
        estimates store `var'
        local lab: variable label `var'
        local nobs = e(N)
        label var `var' "`lab' (`nobs')"

        qui eststo: ivreg2 race_dif i.wave (`var' = i.sic_code), robust
        estimates store `var'_fid
        local fes = "`fes' `var'_fid"
    }

    coefplot (${covars}, label("Within industry")) (`fes', label("Between industry")), drop(_cons *.wave) xline(0) graphregion(color(white)) legend(region(color(white))) xtitle("Effect on job `gap' contact gap")
    if "`touse'" != "over40" {
        graph export ${dir}/figures/figureA7_`touse'.pdf, replace
    }

    * Regular task regs
    eststo clear
    foreach var of global covars {
        qui su `var', d
        replace `var' = (`var' - r(mean))/r(sd)
    }

    local fes = ""
    foreach var of global covars {
        qui eststo: reg race_dif i.wave `var', robust
        estimates store `var'
        local lab: variable label `var'
        local nobs = e(N)
        label var `var' "`lab' (`nobs')"

    }
    eststo: reg race_dif ${covars} i.wave, robust
    estimates store multivariate
    testparm score_*
    local nfp = r(p)

    areg race_dif ${covars} i.wave, absorb(firm_id) robust
    testparm score_*

    coefplot (${covars}, label("Bivariate")) (multivariate, label("Multivariate")), drop(_cons *.wave) xline(0) graphregion(color(white)) legend(region(color(white))) xtitle("Effect on job `gap' contact gap") note("P-value for joint sig w/o firm FE: `: di %3.2f `=`nfp''', w/ firm fe: `: di %3.2f `=r(p)''")
    if "`touse'" != "over40" {
        graph export ${dir}/figures/figure3_`touse'.pdf, replace
    }

    * Estimate regressions with firm covariates
    global covars log_firm_emp sls_per_emp profit_per_emp overall_score viols_per_emp penalties_per_emp fed_contract black_board_share female_board_share firm_share_nonwhite_mgmt firm_share_female_mgmt diversity_officer diversity_score phones_per_job
    replace phones_per_job = -phones_per_job

    * Standardize
    foreach var of global covars {
        if ("`var'" != "fed_contract") & ("`var'" != "diversity_officer") {
            qui su `var', d
            replace `var' = (`var' - r(mean))/r(sd)
        }
    }

    label var log_firm_emp "Log employment"
    label var viols_per_emp "DOL viols / emp" 
    label var penalties_per_emp "Empl-discr cases / emp"
    label var sls_per_emp "Sales / emp" 
    label var profit_per_emp "Profit / emp" 
    label var sales_margin "Profit / sales" 
    label var black_board_share "% board Black"
    label var female_board_share "% board female"
    label var csuite_black_share "% c-suite Black"
    label var csuite_female_share "% c-suite female"
    label var diversity_officer "Has chief diversity officer"
    label var share_black_ind "% ind Black"
    label var share_black_mgmt_ind "% ind mgmt Black"
    label var overall_score "GD score"
    label var diversity_score "GD diversity score"
    label var phones_per_job "Callback centralization"
    label var firm_share_white_mgmt "% managers white"
    label var firm_share_nonwhite_mgmt "% managers non-white"
    label var firm_share_male_mgmt "% managers male"
    label var firm_share_female_mgmt "% managers female"
    label var fed_contract "Federal contractor"

    eststo clear
    foreach var of global covars {
        if "`var'" == "phones_per_job" {
            qui eststo: ivreg2 race_dif i.wave (`var' = `var'_other), cluster(firm_id)  
        }
        else {
            qui eststo: reg race_dif i.wave `var', cluster(firm_id)  
        }
        estimates store `var'
        qui distinct firm_id if `var' != .
        local lab: variable label `var'
        local nobs = r(ndistinct)
        label var `var' "`lab' (`nobs')"
    }
    eststo: ivreg2 race_dif i.wave log_firm_emp viols_per_emp penalties_per_emp sls_per_emp profit_per_emp black_board_share female_board_share overall_score diversity_score firm_share_nonwhite_mgmt firm_share_female_mgmt diversity_officer fed_contract (phones_per_job = phones_per_job_other), cluster(firm_id)       
    estimates store multivariate
    testparm ${covars}

    coefplot (${covars}, label("Bivariate")) (multivariate, label("Multivariate")), drop(_cons *.wave) xline(0) graphregion(color(white)) legend(region(color(white))) xtitle("Effect on job `gap' contact gap") note("P-value for joint significance: `: di %4.3f `=r(p)''") headings(log_firm_emp = "{bf:Firm performance}" viols_per_emp = "{bf:Legal compliance}" black_board_share = "{bf:Firm diversity}" phones_per_job = "{bf:Callback patterns}")
    if "`touse'" != "over40" {
        graph export ${dir}/figures/figure5_`touse'.pdf, replace
    }
    else {
        graph export ${dir}/figures/figureA9.pdf, replace    
    }

    * Restore
    restore
}


*** Regs on posterior gaps
foreach touse of varlist white male {
    use ${dir}/data/data.dta, clear
    keep firm_id naics naics3 sic_code
    duplicates drop

    if "`touse'" == "white" {
        local gap = "white-Black"
    }
    else if "`touse'" == "male" {
        local gap = "male-female"
    }
    else {
        local gap = "under-over 40"        
    }

    merge m:1 naics3 using `eeoc', keep(3 1) nogen
    merge m:1 naics using `concent_naics', keep(3 1) nogen
    merge m:1 sic_code using `rawgap', keep(3 1) nogen
    merge m:1 naics3 using `wagegap_naics', keep(3 1) nogen
    merge m:1 naics3 using `shares_naics', keep(3 1) nogen

    preserve
    insheet using ${dir}/dump/`touse'_posterior_features.csv, comma clear
    keep firm_id post_mean_beta normal_mean_beta 
    tempfile postmeans
    save `postmeans', replace
    restore

    merge 1:1 firm_id using `postmeans', keep(3) nogen

    global covars wage_level_race wage_gap_race share_black_ind mgmt_dif_black_ind col_share_gap_race wage_level_gender wage_gap_gender share_female_ind mgmt_dif_female_ind col_share_gap_gender top4share

    foreach var of global covars {
        qui su `var', d
        replace `var' = (`var' - r(mean))/r(sd)
    }

    * replace top4share = top4share/100
    label var wage_gap_race_basic "White - Black wage"
    label var wage_gap_race "White - Black adj wage"
    label var emp_gap_race "White - Black emp pr"
    label var wage_level_race "White adj wage"
    label var wage_gap_gender_basic "Male - female wage"
    label var wage_gap_gender "Male - female adj wage"
    label var emp_gap_gender "Male - female emp pr"
    label var wage_level_gender "Male adj wage"
    label var share_black_ind "% ind Black"
    label var share_black_mgmt_ind "% ind mgmt Black"
    label var share_black_senior_mgmt_ind "% ind mgmt Black"
    label var mgmt_dif_black_ind "% mgmt - % ind Black"
    label var share_female_ind "% ind female"
    label var share_female_mgmt_ind "% ind mgmt female"
    label var share_female_sr_mgmt_ind "% ind mgmt female"
    label var mgmt_dif_female_ind "% mgmt - % ind female"
    label var top4share "Top 4 sales share"
    label var col_share_gap_race "White - Black col share"
    label var col_share_gap_gender "Male - female col share"

    eststo clear
    foreach var of global covars {
        qui eststo: reg `var' post_mean_beta, robust 
        estimates store ef_`var'

        qui eststo: reg `var' normal_mean_beta, robust 
        estimates store fe_`var'
    }

    coefplot (ef_*, label("Posterior mean")) (fe_*, label("Linear shrinkage")), aseq swapnames eqrename(^ef_(.*)$ = \1 ^fe_(.*)$ = \1, regex) drop(_cons) xline(0) graphregion(color(white)) legend(region(color(white)))  xtitle("Regression coefficient on `gap' gap") headings(wage_level_race = "{bf:Race characteristics}" wage_level_gender = "{bf:Gender characteristics}" top4share = "{bf:Concentration}")
    graph export ${dir}/figures/figure9_`touse'.pdf, replace

}


